package uit.shoeshop.action;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import uit.shoeshop.dto.CategoryDTO;
import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.lib.Constants;
import uit.shoeshop.lib.Util;
import uit.shoeshop.model.ProductModel;

public class ManageproductAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String link;
	private String error;
	private ArrayList<ProductDTO> products;
	private ProductDTO product;
	private HashMap<Integer, String> allCate;
	private String page;
	private String pagination;
	private String paction;
	private String pid;
	private ArrayList<CategoryDTO> parentCate;
	private HashMap<Integer, ArrayList<CategoryDTO>> childCate;
	
	private String title;
	private String desc;
	private String price;
	private String stock;
	private String cate;
	private String enabled;
	private String fileUrl0;
	private String fileUrl1;
	private String fileUrl2;
	private String fileUrl3;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	   public String execute() throws Exception {
	    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
	    	String ret = NONE;
	    	ProductModel productCtrl = new ProductModel();
	    	
	    	setLink(this.request.getRequestURL().toString());
	    	setError("");
	    	boolean check = false;
	    	try{
	        	if(this.request.getMethod().equalsIgnoreCase("POST") && getPaction() != null && getPid() != null  && Util.isIntNumber(getPid())){
	        		if(getPaction().equals("del")){
	        			int checksql = productCtrl.delProduct(Integer.parseInt(getPid()));
	        			if(checksql == 2){
	        				setError("Vui lòng xóa hết hóa đơn có sản phẩm trước khi xóa !");
	        			}else if(checksql == 0){
	        				ret = ERROR;
	        				check = true;
	        			}else{
	        				ret = SUCCESS;
	        				check =true;
	        			}
	        		}else if(getPaction().equals("active")){
	        			int checksql = productCtrl.activeProduct(Integer.parseInt(getPid()),1);
	        			if(checksql == 0){
	        				ret = ERROR;	        				
	        			}else{
	        				ret = SUCCESS;
	        			}
	        			check = true;
	        		}else if(getPaction().equals("inactive")){
	        			int checksql = productCtrl.activeProduct(Integer.parseInt(getPid()),0);
	        			if(checksql == 0){
	        				ret = ERROR;	        				
	        			}else{
	        				ret = SUCCESS;
	        			}
	        			check = true;	        			
	        		}
	        	}
	        	if(check == false){
	        		if(getPage()== null || !Util.isIntNumber(getPage()))
	        			setPage("1");
	        		setPagination(Util.Panigation("/shoeshop/manageproduct.html?page=", productCtrl.countTotalProduct(), Constants.LIMIT_PRODUCT,Integer.parseInt(getPage())));	        		
	        		setProducts(productCtrl.getAllProduct(Integer.parseInt(getPage())));
	        		setAllCate(productCtrl.getAllCategory());
	        	}
	    	}catch(Exception e){
	    		ret= ERROR;
	    	}finally{
	    		productCtrl = null;
	    	}     	
	    	return ret;
	    }

	    public String edit() throws Exception {
	    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
	    	String ret = ERROR;

	    	ProductModel productCtrl = new ProductModel();
	    	try{
	        	if(this.request.getMethod().equalsIgnoreCase("POST") && getPaction() != null && getPid() != null  && Util.isIntNumber(getPid())){
	        		if(getPaction().equals("change")){
	        			setProduct(productCtrl.getProductById(Integer.parseInt(getPid())));
	        			setParentCate(productCtrl.getAllParentCategory());
	            		setChildCate(productCtrl.getAllChildCategory(getParentCate()));	        			
	        			if(getProduct() == null || getParentCate() == null){
	        				throw new Exception("Khong lay duoc thong tin san pham!");
	        			}else{
	        				ret = NONE;
	        			}
	        		}else if(getPaction().equals("process")){
						if (getTitle() != null && getDesc() != null
								&& getFileUrl0() != null && getFileUrl1() != null
								&& getFileUrl2() != null && getFileUrl3() != null
								&& Util.isIntNumber(getPrice())
								&& Util.isIntNumber(getStock())
								&& Util.isIntNumber(getCate())
								&& Util.isIntNumber(getEnabled())) {
							
						if (productCtrl.updateProduct(
								Integer.parseInt(getPid()), getTitle(),
								getDesc(), Integer.parseInt(getPrice()),
								Integer.parseInt(getStock()),
								Integer.parseInt(getCate()),
								Integer.parseInt(getEnabled()), getFileUrl0(),
								getFileUrl1(), getFileUrl2(), getFileUrl3()) == 1) {
							ret = SUCCESS;
						} else {
							throw new Exception("Update san pham bi loi!");
						}
									
						}else{
							throw new Exception("Thong tin update khong day du!");
						}
	        		}else{
	        			throw new Exception("Khong co hanh dong nao duoc thuc hien!");
	        		}
	        	}else{
	        		throw new Exception("Trang khong duoc truyen du lieu!");
	        	}
	    	}catch(Exception e){
	    		ret= ERROR;
	    		e.printStackTrace();
	    	}finally{
	    		productCtrl = null;
	    	}     	
	    	return ret;
	    }    
	    
	    public String add() throws Exception {
	    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
	    	String ret = ERROR;
	    	setLink("manageproduct.html");
	    	ProductModel productCtrl = new ProductModel();
	    	try{
    			setParentCate(productCtrl.getAllParentCategory());
        		setChildCate(productCtrl.getAllChildCategory(getParentCate()));	
	        	if(this.request.getMethod().equalsIgnoreCase("POST") && getPaction() != null ){
	        		if(getPaction().equals("process")){
						if (getTitle() != null && getDesc() != null
								&& getFileUrl0() != null && getFileUrl1() != null
								&& getFileUrl2() != null && getFileUrl3() != null
								&& Util.isIntNumber(getPrice())
								&& Util.isIntNumber(getStock())
								&& Util.isIntNumber(getCate())
								&& Util.isIntNumber(getEnabled())) {
							
						if (productCtrl.insertProduct(
								getTitle(),
								getDesc(), Integer.parseInt(getPrice()),
								Integer.parseInt(getStock()),
								Integer.parseInt(getCate()),
								Integer.parseInt(getEnabled()), getFileUrl0(),
								getFileUrl1(), getFileUrl2(), getFileUrl3()) == 1) {
							ret = SUCCESS;
						} else {
							throw new Exception("Insert san pham bi loi!");
						}
									
						}else{
							throw new Exception("Thong tin insert khong day du!");
						}
	        		}else{
	        			throw new Exception("Khong co hanh dong nao duoc thuc hien!");
	        		}
	        	}else{
	        		ret = NONE;
	        	}
	    	}catch(Exception e){
	    		ret= ERROR;
	    		e.printStackTrace();
	    	}finally{
	    		productCtrl = null;
	    	}     	
	    	return ret;

	    }

		/**
		 * @param link the link to set
		 */
		public void setLink(String link) {
			this.link = link;
		}

		/**
		 * @return the link
		 */
		public String getLink() {
			return link;
		}

		/**
		 * @param products the products to set
		 */
		public void setProducts(ArrayList<ProductDTO> products) {
			this.products = products;
		}

		/**
		 * @return the products
		 */
		public ArrayList<ProductDTO> getProducts() {
			return products;
		}

		/**
		 * @param page the page to set
		 */
		public void setPage(String page) {
			this.page = page;
		}

		/**
		 * @return the page
		 */
		public String getPage() {
			return page;
		}

		/**
		 * @param error the error to set
		 */
		public void setError(String error) {
			this.error = error;
		}

		/**
		 * @return the error
		 */
		public String getError() {
			return error;
		}

		/**
		 * @param allCate the allCate to set
		 */
		public void setAllCate(HashMap<Integer, String> allCate) {
			this.allCate = allCate;
		}

		/**
		 * @return the allCate
		 */
		public HashMap<Integer, String> getAllCate() {
			return allCate;
		}

		/**
		 * @param paction the paction to set
		 */
		public void setPaction(String paction) {
			this.paction = paction;
		}

		/**
		 * @return the paction
		 */
		public String getPaction() {
			return paction;
		}

		/**
		 * @param pid the pid to set
		 */
		public void setPid(String pid) {
			this.pid = pid;
		}

		/**
		 * @return the pid
		 */
		public String getPid() {
			return pid;
		}

		/**
		 * @param product the product to set
		 */
		public void setProduct(ProductDTO product) {
			this.product = product;
		}

		/**
		 * @return the product
		 */
		public ProductDTO getProduct() {
			return product;
		}

		/**
		 * @param parentCate the parentCate to set
		 */
		public void setParentCate(ArrayList<CategoryDTO> parentCate) {
			this.parentCate = parentCate;
		}

		/**
		 * @return the parentCate
		 */
		public ArrayList<CategoryDTO> getParentCate() {
			return parentCate;
		}

		/**
		 * @param childCate the childCate to set
		 */
		public void setChildCate(HashMap<Integer, ArrayList<CategoryDTO>> childCate) {
			this.childCate = childCate;
		}

		/**
		 * @return the childCate
		 */
		public HashMap<Integer, ArrayList<CategoryDTO>> getChildCate() {
			return childCate;
		}

		/**
		 * @param title the title to set
		 */
		public void setTitle(String title) {
			this.title = title;
		}

		/**
		 * @return the title
		 */
		public String getTitle() {
			return title;
		}

		/**
		 * @param desc the desc to set
		 */
		public void setDesc(String desc) {
			this.desc = desc;
		}

		/**
		 * @return the desc
		 */
		public String getDesc() {
			return desc;
		}

		/**
		 * @param price the price to set
		 */
		public void setPrice(String price) {
			this.price = price;
		}

		/**
		 * @return the price
		 */
		public String getPrice() {
			return price;
		}

		/**
		 * @param stock the stock to set
		 */
		public void setStock(String stock) {
			this.stock = stock;
		}

		/**
		 * @return the stock
		 */
		public String getStock() {
			return stock;
		}

		/**
		 * @param cate the cate to set
		 */
		public void setCate(String cate) {
			this.cate = cate;
		}

		/**
		 * @return the cate
		 */
		public String getCate() {
			return cate;
		}

		/**
		 * @param enabled the enabled to set
		 */
		public void setEnabled(String enabled) {
			this.enabled = enabled;
		}

		/**
		 * @return the enabled
		 */
		public String getEnabled() {
			return enabled;
		}

		/**
		 * @param fileUrl0 the fileUrl0 to set
		 */
		public void setFileUrl0(String fileUrl0) {
			this.fileUrl0 = fileUrl0;
		}

		/**
		 * @return the fileUrl0
		 */
		public String getFileUrl0() {
			return fileUrl0;
		}

		/**
		 * @param fileUrl1 the fileUrl1 to set
		 */
		public void setFileUrl1(String fileUrl1) {
			this.fileUrl1 = fileUrl1;
		}

		/**
		 * @return the fileUrl1
		 */
		public String getFileUrl1() {
			return fileUrl1;
		}

		/**
		 * @param fileUrl2 the fileUrl2 to set
		 */
		public void setFileUrl2(String fileUrl2) {
			this.fileUrl2 = fileUrl2;
		}

		/**
		 * @return the fileUrl2
		 */
		public String getFileUrl2() {
			return fileUrl2;
		}

		/**
		 * @param fileUrl3 the fileUrl3 to set
		 */
		public void setFileUrl3(String fileUrl3) {
			this.fileUrl3 = fileUrl3;
		}

		/**
		 * @return the fileUrl3
		 */
		public String getFileUrl3() {
			return fileUrl3;
		}

		/**
		 * @param pagination the pagination to set
		 */
		public void setPagination(String pagination) {
			this.pagination = pagination;
		}

		/**
		 * @return the pagination
		 */
		public String getPagination() {
			return pagination;
		}	
}
